<template>
  <div class="wrapper">
    <img class="wrapper__img" src="http://www.dell-lee.com/imgs/vue3/near.png" />
    <div class="wrapper__input">
      <input class="wrapper__input__content" v-model="username" placeholder="请输入用户名"/>
    </div>
    <div class="wrapper__input">
      <input class="wrapper__input__content" v-model="password" type="password"  placeholder="请输入密码" />
    </div>
    <div class="wrapper__login-button" @click="handleClick">
      登录
    </div>
    <div class="wrapper__login-link" @click="resisterClick">
      立即注册
    </div>
    <Toast v-if="show" :message="toastMessage"/>
  </div>
</template>

<script >
import { useRouter } from 'vue-router'
import { reactive, toRefs } from 'vue'
import { post } from '../../utils/request'
import Toast, { userToastEffect } from '../../components/Toast.vue'
const useLoginEffect = (showToast) => {
  const router = useRouter()
  const data = reactive({
    username: '',
    password: ''
  })
  const handleClick = async () => {
    try {
      const result = await post('/api/user/login', data)
      if (result?.errno === 0) {
        localStorage.isLogin = true
        router.push({ name: 'Home' })
      } else {
        showToast('登录失败')
      }
    } catch (e) {
      showToast('请求失败')
    }
  }
  const { username, password } = toRefs(data)
  return { username, password, handleClick }
}
const useRegisterEffect = () => {
  const router = useRouter()
  const resisterClick = () => {
    router.push({ name: 'Register' })
  }
  return { resisterClick }
}
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'Login',
  components: { Toast },
  // 指责就是代码执行的一个流程
  setup () {
    const { toastMessage, showToast, show } = userToastEffect()
    const { username, password, handleClick } = useLoginEffect(showToast)
    const { resisterClick } = useRegisterEffect()
    return { handleClick, resisterClick, username, password, toastMessage, show }
  }
}
</script>

<style lang="scss" scoped>
.wrapper{
  //position: absolute;
  //top: 50%;
  //left: 0;
  //right: 0;
  //transform: translateY(-50%);
  position: absolute;
  top: 25%;
  left: 0;
  right: 0;
  height: 100%;
  &__img{
    width: .66rem;
    height: .66rem;
    display: block;
    margin:  0 auto .4rem auto;
  }
  &__input{
    box-sizing: border-box;
    height: .48rem;
    margin: 0 .4rem .16rem .4rem;
    padding: 0 .16rem;
    background: #F9F9F9;
    border: 1px solid rgba(0,0,0,.1);
    border-radius: 6px;
    &__content{
      width: 100%;
      border: none;
      outline: none;
      line-height: .48rem;
      background: none;
      font-size: .16rem;
      color: rgba(0,0,0,.5);
      &::placeholder{
        color: rgba(0,0,0,.5);
      }
    }
  }
  &__login-button{
    background: #0091FF;
    box-shadow: 0 .04rem .08rem 0 rgba(0,145,255,.32);
    border-radius: 4px;
    color: #fff;
    margin: .32rem .4rem .16rem .4rem;
    line-height: .48rem;
    font-size: .16rem;
    text-align: center;
  }
  &__login-link{
    font-size: .14rem;
    color: rgba(0,0,0,.5);
    text-align: center;
  }
}
</style>
